package com.tianruan.dc.modules.sys.user.security;

import com.tianruan.dc.common.utils.response.VResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

/**
 * @PS
 */
@Slf4j
public class Auth2ResponseExceptionTranslator implements WebResponseExceptionTranslator<VResponse<Object>> {

    @Override
    public ResponseEntity<VResponse<Object>> translate(Exception e) {
        Throwable throwable = e.getCause();
        if (throwable instanceof InvalidTokenException) {
            return ResponseEntity.status(((InvalidTokenException) throwable).getHttpErrorCode()).body(VResponse.error(10001 , "token失效" + e.getMessage()));
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(VResponse.error(1000 , e.getMessage()));
    }
}